#include "ui.h"

//#include "oled.h"


#define Page0_Item_Max 	(sizeof(Page0_Item)/sizeof(Page0_Item[0])-1)
#define Page1_Item_Max 	(sizeof(Page1_Item)/sizeof(Page1_Item[0])-1)
#define Page2_Item_Max  (sizeof(Page2_Item)/sizeof(Page2_Item[0])-1)
#define Page3_Item_Max  (sizeof(Page3_Item)/sizeof(Page3_Item[0])-1)
#define Page4_Item_Max  (sizeof(Page4_Item)/sizeof(Page4_Item[0])-1)

static int temp;
Item_Lib const Page0_Item[] =
{
	"zhijiaowan",&temp,Float,RW,1,1,
	"ad0",&temp,Float,RW,1,1,
	"ad1",&temp,Float,RW,1,1,
	"ad2",&temp,Float,RW,1,1,
	"ad3",&temp,Float,RW,1,1,
	"ad4",&temp,Float,RW,1,1,
};

Item_Lib const Page1_Item[] =
{
	"jishu",&temp,Float,RW,1,1,
	"huandao_flag",&temp,Float,RW,1,1,
	"jinhuan_flag",&temp,Float,RW,1,1,
	"jishu1",&temp,Float,RW,1,1,
	"js1",&temp,Float,RW,1,1,
};

Item_Lib const Page2_Item[] =
{
    "a0",&temp,Float,RW,1,1,
    "a1",&temp,Float,RW,1,1,
    "a2",&temp,Float, RW,1,1,
    "a3",&temp,Float, RW,1,1,
    "a4",&temp,Float, RW,1,1,
    "a5",&temp,Float,RW,1,1,
    "a6",&temp,Float,RW,1,1,
};

Item_Lib const Page3_Item[] =
{
	"duoji",&temp,Float,RW,1,1,
	"ked",&temp,Float,RW,1,1,
	"kep",&temp,Float,RW,1,1,
};

Item_Lib const Page4_Item[] =
{
	"pwm",&temp,Float,RW,1,1,
	"encoder",&temp,Float,RW,1,1,

	"kd",&temp,Float,RW,1,1,
	"kp",&temp,Float,RW,1,1,
	"ki",&temp,Float,RW,1,1,
};

Page_Lib const Page[] =
{
	{"g1h", (Item_LibPtr)Page0_Item, Page0_Item_Max},
	{"huandao", (Item_LibPtr)Page1_Item, Page1_Item_Max},
	{"diangan", (Item_LibPtr)Page2_Item, Page2_Item_Max},
	{"pid", (Item_LibPtr)Page3_Item, Page3_Item_Max},
    {"dianji", (Item_LibPtr)Page4_Item, Page4_Item_Max},

};





UI_CMD_e UI_CMD;

Key_Type_e Key_Detected;

Active_Type Active;

uint16_t const Page_MAX = (sizeof(Page) / sizeof(Page[0]) - 1);

void UI_Init(void) 
{
	//KEY_Init();
	Active.ShowGraphic = false;
	Active.Accuracy = 10;
	Active.PageNum = 0;
	Active.ItemNum = 0;
	Active.PagePtr = (Page_LibPtr) Page + Active.PageNum;
	Active.ItemPtr = (Active.PagePtr->ItemPtr + Active.ItemNum);
	Key_Detected = Key_None;
	UI_CMD = UI_None;
}

uint32_t Save_Addr;
int16_t Page_Num,Item_Num;

uint32_t Read_Addr;


void Page_Plus(void) 
{
	Active.ShowGraphic = false;
	Active.PageNum++;
	if (Active.PageNum > Page_MAX)
		Active.PageNum = 0;
	Active.PagePtr = (Page_LibPtr) Page + Active.PageNum;
	Active.ItemNum = 0;
	Active.ItemPtr = Active.PagePtr->ItemPtr + Active.ItemNum;
}

void Page_Minus(void) 
{
	Active.ShowGraphic = false;
	Active.PageNum--;
	if (Active.PageNum < 0)
		Active.PageNum = Page_MAX;
	Active.PagePtr = (Page_LibPtr) Page + Active.PageNum;
	Active.ItemNum = 0;
	Active.ItemPtr = Active.PagePtr->ItemPtr + Active.ItemNum;
}

void Item_Plus(void) 
{

	Active.ShowGraphic = false;
	Active.ItemNum++;
	if (Active.ItemNum > Active.PagePtr->Item_Max)
		Active.ItemNum = 0;
	Active.ItemPtr = Active.PagePtr->ItemPtr + Active.ItemNum;
}

void Item_Minus(void) 
{
	Active.ShowGraphic = false;
	Active.ItemNum--;
	if (Active.ItemNum < 0)
		Active.ItemNum = Active.PagePtr->Item_Max;
	Active.ItemPtr = Active.PagePtr->ItemPtr + Active.ItemNum;
}

void Data_Plus(void) 
{
	switch (Active.ItemPtr->Data_Attribute.Type) {
	case Double:
		*(double*) Active.ItemPtr->DataPtr += (double)Active.ItemPtr->add_value;
		break;
	case Float:
		*(float*) Active.ItemPtr->DataPtr += (float)Active.ItemPtr->add_value;
		break;
	case Int32_t:
		*(int32_t*) Active.ItemPtr->DataPtr += (int32_t) Active.ItemPtr->add_value;
		break;
	case Uint32_t:
		*(uint32_t*) Active.ItemPtr->DataPtr += (uint32_t)Active.ItemPtr->add_value;
		break;
	case Int16_t:
		*(int16_t*) Active.ItemPtr->DataPtr += (int16_t)Active.ItemPtr->add_value;
		break;
	case Uint16_t:
		*(uint16_t*) Active.ItemPtr->DataPtr += (uint16_t) Active.ItemPtr->add_value;
		break;
	case Int8_t:
		*(int8_t*) Active.ItemPtr->DataPtr += (int8_t) Active.ItemPtr->add_value;
		break;
	case Uint8_t:
		*(uint8_t*) Active.ItemPtr->DataPtr += (uint8_t) Active.ItemPtr->add_value;
		break;
	case Function:
		Active.ShowGraphic = true;
		break;
	default:
		break;
	}
}

void Data_Minus(void) 
{

	switch (Active.ItemPtr->Data_Attribute.Type) {
	case Double:
		*(double*) Active.ItemPtr->DataPtr -= (double) Active.ItemPtr->cut_value;
		break;
	case Float:
		*(float*) Active.ItemPtr->DataPtr -= (float) Active.ItemPtr->cut_value;
		break;
	case Int32_t:
		*(int32_t*) Active.ItemPtr->DataPtr -= (int32_t)Active.ItemPtr->cut_value;
		break;
	case Uint32_t:
		*(uint32_t*) Active.ItemPtr->DataPtr -= (uint32_t) Active.ItemPtr->cut_value;
		break;
	case Int16_t:
		*(int16_t*) Active.ItemPtr->DataPtr -= (int16_t) Active.ItemPtr->cut_value;
		break;
	case Uint16_t:
		*(uint16_t*) Active.ItemPtr->DataPtr -= (uint16_t) Active.ItemPtr->cut_value;
		break;
	case Int8_t:
		*(int8_t*) Active.ItemPtr->DataPtr -= (int8_t) Active.ItemPtr->cut_value;
		break;
	case Uint8_t:
		*(uint8_t*) Active.ItemPtr->DataPtr -= (uint8_t) Active.ItemPtr->cut_value;
		break;
	case Function:
		Active.ShowGraphic = false;
		break;
	default:
		break;
	}
}

void UI_Process(void) 
{
	switch (UI_CMD) {
	case UI_Value_Plus:
		Data_Plus();
		break;
	case UI_Value_Minus:
		Data_Minus();
		break;
	case UI_Page_Plus:
		Page_Plus();
		break;
	case UI_Page_Minus:
		Page_Minus();
		break;
	case UI_Item_Plus:
		Item_Plus();
		break;
	case UI_Item_Minus:
		Item_Minus();
		break;
	default:
		break;
	}
}

void UI_ShowData(u8 y,Item_LibPtr ItemPtr)
{
    lcd_showstr(10,y,ItemPtr->Name);
    switch (ItemPtr->Data_Attribute.Type) {
	case Float:
		lcd_showfloat(60, y, (int)(float) *(float*) ItemPtr->DataPtr);
		break;
	case Int32_t:
		lcd_showint32(60, y, (int32_t) *(int32_t*) ItemPtr->DataPtr);
		break;
	case Uint32_t:
		lcd_showint32(60, y, (int32_t) *(int32_t*) ItemPtr->DataPtr);
		break;
	case Int16_t:
		lcd_showint16(60, y, (int32_t) *(int16_t*) ItemPtr->DataPtr);
		break;
	case Uint16_t:
		lcd_showint16(60, y, (int32_t) *(uint16_t*) ItemPtr->DataPtr);
		break;
	}
}

void UI_PutMsg(void) 
{
	if(UI_CMD != UI_None)
	//    lcd_clear(WHITE);

	lcd_showstr(0, 0, Active.PagePtr->Name);
	lcd_showfloat(60,0,Active.Accuracy);
	if (Active.ShowGraphic == 0) 
	{
		if (Active.ItemNum < 6) 
		{
			lcd_showstr(0, (Active.ItemNum + 1), ">");
			UI_ShowData( 1,Active.PagePtr->ItemPtr);
			if (Active.PagePtr->Item_Max<1) 
						{return;}
			UI_ShowData( 2,Active.PagePtr->ItemPtr + 1);
			if (Active.PagePtr->Item_Max<2) 
						{return;}
			UI_ShowData(3,Active.PagePtr->ItemPtr + 2);
						if (Active.PagePtr->Item_Max<3) 
						{return;}
			UI_ShowData( 4,Active.PagePtr->ItemPtr + 3);
						if (Active.PagePtr->Item_Max<4) 
						{return;}
			UI_ShowData( 5,Active.PagePtr->ItemPtr + 4);
						if (Active.PagePtr->Item_Max<5) 
						{return;}
			UI_ShowData( 6,Active.PagePtr->ItemPtr + 5);
						
		}
		else 
		{
			lcd_showstr(3,1, "A>");
			UI_ShowData( 1,Active.ItemPtr - 5);
			UI_ShowData( 2,Active.ItemPtr - 4);
			UI_ShowData( 3,Active.ItemPtr - 3);
			UI_ShowData( 4,Active.ItemPtr - 2);
			UI_ShowData( 5,Active.ItemPtr - 1);
			UI_ShowData( 6,Active.ItemPtr);
		}
	}
	else 
	{
		(*(tIsrFunc) Active.ItemPtr->DataPtr)();
	}
}


void UI_Main(void) 
{
	UI_CMD_e key_num=(UI_CMD_e) key_scan();
	UI_Process();
	UI_PutMsg();
}
void show_init(void)
{
	lcd_showstr(0, 1,"mode");
}
void UI_Main1(void) 
{
	
	UI_CMD_e key_num=(UI_CMD_e) key_scan();
	
}
